package library;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import library.entity.FastdfsEntity;
import library.fsds.FastDFSClientUtils;

import java.io.File;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void reDownloadFile(CopyOnWriteArrayList<FastdfsEntity> fastdfsEntityList){
        CopyOnWriteArrayList<FastdfsEntity> sendFastdfsEntityList = new CopyOnWriteArrayList<>();
        for(FastdfsEntity fastdfsEntity : fastdfsEntityList){
            try {
                int re = FastDFSClientUtils.downloadFile(fastdfsEntity.getPhone_record_address(),fastdfsEntity.getPhone_record_file());
                if (re==0){
                    FastDFSClientUtils.delete("group1",fastdfsEntity.getPhone_record_address());
                }
                fastdfsEntityList.remove(fastdfsEntity);
            } catch (Exception e){
                sendFastdfsEntityList.add(fastdfsEntity);
            }
        }
        if (sendFastdfsEntityList.size() != 0){
            reDownloadFile(sendFastdfsEntityList);
        }
    }

    public static void main( String[] args )
    {
        if (args.length == 2){
            ExcelReader reader = ExcelUtil.getReader(FileUtil.file(args[0]));

            List<FastdfsEntity> fastdfsEntityList = reader.readAll(FastdfsEntity.class);
            CopyOnWriteArrayList<FastdfsEntity> sendFastdfsEntityList = new CopyOnWriteArrayList<>(fastdfsEntityList);
            for(FastdfsEntity fastdfsEntity : sendFastdfsEntityList){
                System.out.println(fastdfsEntity.getPhone_record_address());
                String fileName = StrUtil.subAfter(fastdfsEntity.getPhone_record_address(),"/",true);
                System.out.println(fileName);
                String fullPath = args[1]+"\\"+fileName;
                File file = new File(fullPath);
                fastdfsEntity.setPhone_record_file(file);
            }
            reDownloadFile(sendFastdfsEntityList);
        }else {
            System.out.println("参数输入错误");
        }
    }
}
